package org.ccay.manage.log.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.audit.dao.IAuditLogDao;
import org.ccay.audit.vo.LogVO;
import org.ccay.core.base.PageVO;
import org.ccay.core.base.PagedResult;
import org.ccay.core.exception.ApplicationException;
import org.ccay.core.log.Audit;
import org.ccay.core.util.DateUtil;
import org.ccay.core.validator.Validate;
import org.ccay.excel.exporter.service.IExcelExportAssistant;
import org.ccay.manage.log.service.ILogQueryService;
import org.ccay.registry.util.RegistryQueryUtil;

/**
 * 审计日志查询服务的实现
 * 
 * Created：2011-9-8
 * 
 * @author chaowangbang
 * @version:
 */
@Named
public class LogQueryService implements ILogQueryService {
	
	
	/**
	 * 数据字典里用户登录时间段URL
	 */
	private static final String URL ="Ccay.Logs.LoginQueryDateArea";
	
	@Inject
	private IAuditLogDao logDao;
	
	@Inject
	private IExcelExportAssistant localExcelExportAssistant;
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ccay.log.service.ILogQueryService#findLogList(org.ccay.log.LogVO,
	 *      www.ccay.org.ccay.core.base.PageVO)
	 */
	@Validate
	public PagedResult<LogVO> findLogList(LogVO logVO, PageVO pageVO) {
		// 查询记录数
		return logDao.findLogList(logVO, pageVO);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ccay.log.service.ILogQueryService#findTwoLogs(org.ccay.log.LogVO)
	 */
	public List<LogVO> findTwoLogs(LogVO logVO) {
		return logDao.findTwoLogs(logVO);
	}

	/**
	 * 初始化登录时间段，默认不超过3个月
	 * 
	 * @return
	 * @throws ApplicationException
	 */
	public List<String> findPostponement() throws ApplicationException {
		//获取用户登录时间段
		int number = 1;
		String day ="";
		List<String> list = new ArrayList<String>();
		String date = DateUtil.addMonth(0,new Date()).substring(0,10);
		number = RegistryQueryUtil.getIntegerValue(URL);
		if(number >0){
			day= DateUtil.addMonth(-number,new Date()).substring(0,10);
		}else{
			day = DateUtil.addMonth(-1,new Date()).substring(0,10);			
		}			
		list.add(day);
		list.add(date);
		return list;
	}
	
	/**
	 * 分页查询统计用户登录次数
	 * 
	 * @author chaowangbang
	 * @since 2012-05-31
	 * @param page
	 * @return
	 */
	@Validate
	public PagedResult<LogVO> findUserStatisticsList(LogVO logVO,PageVO page){
		return logDao.findUserStatisticsList(logVO, page);
	}
	
	/**
	 * export用户登录次数
	 * 
	 * @author chaowangbang
	 * @since 2012-06-7
	 * @param logVO
	 * @return
	 * @throws ApplicationException 
	 */
	@Audit(module="Logs Operation",operation="export userEnterStat",message="exprot userEnterStat successfully")
	public void exportUserEnterStat(LogVO logVO) throws ApplicationException{
		localExcelExportAssistant.submitExportTask("ccay.logs.userEnterStat", logVO);
	}
	
	/**
	 * export日志
	 * @author chaowangbang
	 * @since 2013-01-11
	 * @param logVO
	 * @return
	 * @throws ApplicationException 
	 */
	@Audit(module="Logs Operation",operation="export logs",message="export logs successfully")
	public void exportLogs(LogVO logVO) throws ApplicationException{
		localExcelExportAssistant.submitExportTask("ccay.logs.log", logVO);
	}
	
	@Validate
	public PagedResult<LogVO> findDifLogList(LogVO logVO, PageVO pageVO) {
		return logDao.findLogList(logVO, pageVO);
	}

}
